package wqh.controller.file;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import wqh.domain.Device;
import wqh.global.GlobalSession;
import wqh.pojo.DeviceJsonPojo;
import wqh.pojo.MapRet;
import wqh.pojo.xkLog;
import wqh.service.DeviceService;
import wqh.service.SystemLogService;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by devts on 2016/11/14.
 */
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/export/device")
public class DeviceExport {

    @Autowired
    private DeviceService deviceService;

    @Autowired
    private SystemLogService systemLogService;

    @RequestMapping(value = "/getAll",method = RequestMethod.GET)
    public Object getAll(HttpServletRequest request, HttpServletResponse response)
    {
        try {
            String sessionID = request.getSession().getId();
            String userName = GlobalSession.getUserNameBySessionID(sessionID);
            if(userName == null) userName = "空用户";
            List<Device> deviceList = deviceService.findAllRecord();
            if(deviceList.size() == 0) return MapRet.SQL_ERROR("没有数据");
            List<DeviceJsonPojo> deviceJsonPojoList = new ArrayList<DeviceJsonPojo>();
            for(Device device : deviceList){
                deviceJsonPojoList.add(new DeviceJsonPojo(device));
            }
            String[] columns = {"用户信息传输装置名称","用户信息传输装置IP","用户信息传输装置描述","值班室电话"};
            HSSFWorkbook book = new HSSFWorkbook();
            HSSFSheet sheet = book.createSheet();
            HSSFRow headRow = sheet.createRow(0);
            for(int i = 0; i < columns.length; i++){
                HSSFCell cell = headRow.createCell(i);
                cell.setCellValue(columns[i]);
                sheet.setColumnWidth(i,38*150);
            }
            for(int i = 1; i < deviceJsonPojoList.size() + 1; i++){
                DeviceJsonPojo d = deviceJsonPojoList.get(i - 1);
                HSSFRow nextRow = sheet.createRow(i);
                HSSFCell cell = nextRow.createCell(0);
                cell.setCellValue(d.getName());
                cell = nextRow.createCell(1);
                cell.setCellValue(d.getIp());
                cell = nextRow.createCell(2);
                cell.setCellValue(d.getDescription());
                cell = nextRow.createCell(3);
                cell.setCellValue(d.getPhone());
            }
            String fileName = "用户信息传输装置列表.xls";
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            book.write(os);
            byte[] content = os.toByteArray();
            InputStream is = new ByteArrayInputStream(content);
            response.reset();
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment;filename="+ new String((fileName).getBytes(), "iso-8859-1"));
            ServletOutputStream out = response.getOutputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            BufferedOutputStream bos = new BufferedOutputStream(out);
            byte[] buff = new byte[4096];
            int bytesRead;
            while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
                bos.write(buff, 0, bytesRead);
            }
            bis.close();
            bos.close();

            xkLog.addLog(systemLogService,"导出 " + fileName,xkLog.getFileConfig(),userName);

            return MapRet.FILE_DOWNLOAD_SUCCESS();
        }catch (Exception e){
            e.printStackTrace();
            return MapRet.FILE_DOWNLOAD_ERROR();
        }
    }

}
